/**
 * Created by Administrator on 2017/7/27.
 */
/**
 * Author: Liu.Z.F
 * Time: 2013-11-21 11:55
 * Copyright (C) 2013 Xiamen Yaxon Networks CO.,LTD.
 */
/**
 * Author: Liu.Z.F
 * Time: 2013-11-18 19:35
 * Copyright (C) 2013 Xiamen Yaxon Networks CO.,LTD.
 */

/**
 * 用户管理－添加
 * @param corprationId
 * @param groupId
 * @param callBack
 * @constructor
 */

Ext.onReady(function(){


YYS_AddUser=function(corprationId, groupId,groupName,callBack){

    var designer = new YYS_AddUser_Designer(corprationId, groupId,groupName,callBack);
    designer.show();

}



function YYS_AddUser_Designer(corprationId, groupId,groupName,successCallBack) {
    this.corporationId = corprationId;
    this.groupId=groupId;
    this.chk_Chief;
    this.form;
    this.successCallBack = successCallBack == undefined ? {} : successCallBack;
    this.window;
    this.controlWidth=150;
    this.groupName=groupName;
    this.typeID = 1;
    this.InitComponent();

}

YYS_AddUser_Designer.prototype={

    //添加角色
    addRecord: function() {
        if (this.combo_role.getValue() == "") {
            Ext.MessageBox.alert(resourceMsgTitle, '请选择角色。');
            return;
        }
        for (var i = 0; i < this.gridStore.getCount(); i++) {
            var r = this.gridStore.getAt(i);
            if (r.data.id == this.combo_role.getValue()) {
                Ext.MessageBox.alert(resourceMsgTitle, "对不起，该角色已经添加");
                return;
            }
        }
        var record = new this.GridRecord({
            id: this.combo_role.getValue(),
            name: this.combo_role.getRawValue()});

        this.gridStore.add(record);
    },
    //移除角色
    deleteRecord: function() {
        if (this.selectRow != undefined) {
            this.gridStore.remove(this.selectRow);
        }
    },

    gridRowClick: function(sm, index, row) {
        this.selectRow = row;
    },

    checkRoleSettings:function()
    {
        var valid = false;
        if (this.gridStore.getCount()>0)
        {
            valid = true;
            this.lstRoles=[];//="[";
            for(var i = 0; i < this.gridStore.getCount(); i++)
            {
                var record = this.gridStore.getAt(i);
                // this.lstRoles=this.lstRoles+(record.data.id)+",";
                this.lstRoles.push(record.data.id);
            }
            // this.lstRoles=this.lstRoles+"]";
        }
        return valid;

    },
    /**
     * 保存信息
     */
    saveHandler:function(){

        //判断密码和确认密码是否一致
        var pwd = this.txt_Password.getValue();
        var confirmPwd = this.txt_ConfirmPassword.getValue();
        if (pwd!=confirmPwd)
        {
            Ext.MessageBox.alert(resourceMsgTitle,'密码与确认密码不一致，请确认输入');
            return;
        }
        //判断是否设置角色
        if (!this.checkRoleSettings())
        {
            Ext.MessageBox.alert(resourceMsgTitle,'用户角色设置不能为空，请设置角色');
            return;
        }
        if(this.form.getForm().isValid())
        {

            //弹出效果
            Ext.MessageBox.show({
                msg: '正在保存，请稍等...',
                progressText: 'Saving...',
                width: 300,
                wait: true,
                waitConfig: { interval: 200 },
                icon: 'download',
                animEl: 'saving'
            });

            setTimeout(function() { }, 1000);
            //this.form.form.submit({
            Ext.Ajax.request({
                url: $("#ctx").val() + "/purview/user/addUser.action",
                method: "POST",
                params: {
                    'addUserReq.loginName':this.txt_LoginName.getValue(),
                    'addUserReq.userName':this.txt_UserName.getValue(),
                    'addUserReq.password':this.txt_Password.getValue(),
                    'addUserReq.effectDate':this.df_serv_start_time.getValue(),
                    'addUserReq.invalidDate':this.df_serv_end_time.getValue(),
                    'addUserReq.autoFrozen':this.ck_auto_frozen.checked?1:0,
                    'addUserReq.postId':this.combo_post.getValue(),
                    'addUserReq.orgId':this.groupId,
                    'addUserReq.roleIds':this.lstRoles,
                    'addUserReq.tid':this.corporationId,
                    'addUserReq.typeID':1//this.typeID//
                    //typeID:1

                },
                success: function(response, options) {
                    var resultObject = Ext.util.JSON.decode(response.responseText);
                    //成功后
                    var flag = resultObject.flag; //Ext.util.JSON.decode(action.response.responseText).flag;

                    if (flag == 0 ) {
                        Ext.MessageBox.alert(resourceMsgTitle, resourceAddSucce);
                        //重新加载列表
                        if (typeof (this.successCallBack) == "function")
                            this.successCallBack();//.successCallBack(returnId, this.txt_GroupName.getValue());
                        this.window.close();
                    }
                    else
                    {
                        Ext.MessageBox.alert(resourceMsgTitle, resultObject.retMsg);
                    }


                } .createDelegate(this),
                failure: function(response, options) {
                    Ext.MessageBox.alert("提示", "添加失败。");
                }
            });
        }
    },

    cancelHandler: function() {
        this.window.close();
    },

    onShow: function() {
        HideWaitingHint();
    },

    show: function() {
        this.window.show();
    },

    close: function() {
        this.window.close();
    }
}


YYS_AddUser_Designer.prototype.InitComponent=function(){
    //显示提示信息
    //if(!Ext.isIE){Ext.QuickTips.init(); }
    //显示提示信息的位置
    // Ext.form.Field.prototype.msgTarget = 'side';
    //登录帐号
    this.txt_LoginName = new Ext.form.TextField({
        name: "loginname",
        fieldLabel: "<font  style='color:red'>登录帐号</font>",
        width: this.controlWidth,
        allowBlank: false,
        blankText: "用户帐号不允许为空"
        //labelStyle: "color:red;"
    });

    //用户姓名
    this.txt_UserName = new Ext.form.TextField({
        name: "username",
        fieldLabel: "<font  style='color:red'>姓名</font>",
        width: this.controlWidth,
        allowBlank: false,
        blankText: "姓名不允许为空"
        //labelStyle: "color:red;"
    });



    //所属企业
    var tenantfields = ["id", "name"];
    this.tenantStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: $("#ctx").val() + '/purview/vehicle_mgr/fetchAllTenantInfo.action',
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: tenantfields,
            root: "tenantInfoList",
            id: "id"
        }),
        autoLoad: true

    });
    this.combo_tname = new Ext.form.ComboBox({
        fieldLabel: "所属企业",
        id: 'tnameCombo',
        name:'tnameCombo',
        store: this.tenantStore,
        hiddenName: 'tnameComboh',
        valueField: 'id',
        displayField: 'name',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true,
        forceSelection: true,
        width: this.controlWidth,
        disabled: true

    });


    //所属岗位
    var postfields = ["id","name"];
    this.postStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: $("#ctx").val() + '/purview/post/fetchPostByGroupID.action',
            params:{"postReq.orgId":this.groupId },
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: postfields,
            root: "postList",
            id: "id"
        }),
        autoLoad: false
    });
    this.postStore.load({params:{"postReq.orgId":this.groupId }});
    this.combo_post = new Ext.form.ComboBox({
        fieldLabel: "<font style='color:red;'>所属岗位</font>",
        name: 'postCombo',
        store: this.postStore,
        hiddenName: 'id',
        valueField: 'id',
        displayField: 'name',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true,
        forceSelection: true,
        width:this.controlWidth,
        allowBlank:false,
        editable: false
    });


    this.btn_Add = new Ext.Button({
        text: '11111',
        iconCls: "addicon",

    });





    //基本信息左边栏
    var UserInfoLeftColumn = {
        columnWidth: .5,
        layout: 'form',
        baseCls: "x-plain",
        items: [

            this.txt_UserName,
            this.txt_Password,
            this.txt_ConfirmPassword,
            this.df_serv_start_time,
            this.combo_tname

        ]
    };


    //基本信息右边栏
    var UserInfoRightColumn = {
        columnWidth: .5,
        layout: 'form',
        baseCls: "x-plain",
        items: [

            this.txt_LoginName,
            this.combo_post,
            this.txt_group,
            this.df_serv_end_time,
            this.ck_auto_frozen



        ]
    };


    //表单
    this.form = new Ext.form.FormPanel({
        width: 510,
        height:370,
        labelAlign: "right",
        labelWidth: 65,
        plain: true,
        layout: "form",
        baseCls: "x-plain",
        buttonAlign: "center",
        bodyStyle: "padding:10px 0 0 0",
        items: [{layout:'column',

            items:[UserInfoLeftColumn,  UserInfoRightColumn]},
        ]
    });


    //窗体
    this.window = new Ext.Window({
        title: "添加用户信息",
        width: 530,
        height:380,
        plain: true,
        iconCls: "addicon",
        //不可以随意改变大小
        resizable: false,
        //是否可以拖动
        draggable: true,
        defaultType: "textfield",
        labelWidth: 100,
        labelAlign: "right",
        collapsible: true, //允许缩放条
        closeAction: 'close',
        closable: true,
        //弹出模态窗体
        modal: 'true',
        buttonAlign: "center",
        items: [this.form],

        buttons: [{
            text: "保存",
            minWidth: 70,

        }
//        , {
//            text: "重置",
//            minWidth: 70,
//            handler: this.resetHandler.createDelegate(this)
//        }
            , {
                text: "取消",
                minWidth: 70,

            }]
    });

}

YYS_AddUser('panel','panel','value');
});